package com.sky.controller;

import com.sky.anotion.ValidId;
import com.sky.anotion.validator.UpdateGroup;
import com.sky.constant.MessageConstant;
import com.sky.dto.DepartmentAddDTO;
import com.sky.dto.common.PageRequest;
import com.sky.dto.common.ParamsID;
import com.sky.dto.common.ParamsStatus;
import com.sky.dto.common.QueryParams;
import com.sky.entity.Department;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/department")
@Slf4j
@Validated
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    /**
     * 分页获取获取所有部门列表
     * @return 返回记录条数和recode数据
     */
    @PostMapping("/list")
    public Result<PageResult> getAllDepartmentsList(@RequestBody PageRequest<QueryParams> queryParams) {
        PageResult departments = departmentService.getAllDepartments(queryParams);
        return Result.success(departments, MessageConstant.OK_MESSAGE);
    }
    /**
     * 新增部门
     */
    @PostMapping("/add")
    public Result addDepartment(@RequestBody DepartmentAddDTO departmentAddDTO) {
        log.info("新增部门: {}", departmentAddDTO);
        departmentService.addDepartment(departmentAddDTO);
        return Result.success(MessageConstant.OK_MESSAGE);
    }

    /**
     * 删除部门
     * @param ids 部门id
     * @return
     */
    @DeleteMapping("/delete")
    public Result deleteDepartment(@RequestBody ParamsID ids) {
        log.info("删除部门，ids: {}", ids);
        departmentService.deleteDepartment(ids);
        return Result.success(MessageConstant.OK_MESSAGE);
    }

    /**
     * 更新部门信息
     * @param department
     * @return
     */
    @PutMapping("/update")
    public Result updateDepartment(@RequestBody @Validated(UpdateGroup.class) Department department) {
        departmentService.updateDepartment(department);
        return Result.success(MessageConstant.OK_MESSAGE);
    }
    /**
     * 获取部门数据 所有以启用的部门数据 tree构造专用
     * @return 返回数组List
     */
    @PostMapping("/getAll")
    public Result<List<Department>> getAllDepartments(@RequestBody QueryParams queryParams) {
        log.info("获取所有部门列表，queryParams: {}", queryParams);
        List<Department> departments = departmentService.getAllDepartmentsList(queryParams);
        return Result.success(departments, MessageConstant.OK_MESSAGE);
    }

    /**
     * 获取一级部门的信息
     * @return
     */
    @PostMapping("/getAllParent")
    public Result<List<Department>> getAllParentDepartments() {
        List<Department> departments = departmentService.getDepartmentPrent();
        return Result.success(departments, MessageConstant.OK_MESSAGE);
    }
    /**
     * 修改部门启用状态
     */
    @PostMapping("/updateStatus")
    public Result<String> updateDepartmentStatus(@RequestBody @Validated(UpdateGroup.class) ParamsStatus paramsStatus) {
        log.info("修改部门启用状态，paramsID: {}", paramsStatus);
        departmentService.updateDepartmentStatus(paramsStatus);
        return Result.success(MessageConstant.OK_MESSAGE);
    }
}
